#include "commonpage.h"
#include "listitembox.h"
#include "ui_commonpage.h"

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    // 不要水平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &image)
{
    // 设置标题
    ui->pageTitle->setText(title);
    // 设置封面栏
    ui->musicImageLabel->setPixmap(QPixmap(image));
    // 设置label中的照片缩放
    ui->musicImageLabel->setScaledContents(true);


    // 测试
//    ListItemBox* listItemBox = new ListItemBox(this);
//    QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
//    listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
//    ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);
}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType = pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    // 将旧内容清空
    musicListOfPage.clear();

    for(auto& music : musicList)
    {
        switch(pageType)
        {
        case LOCAL_PAGE:
            musicListOfPage.push_back(music.getMusicId());
            break;
        case LIKE_PAGE:
            if(music.getIsLike())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
        default:
            break;
        }
    }
}

void CommonPage::reFresh(MusicList &musicList)
{
    // 从musicList中分离出当前页面的所有音乐
    addMusicToMusicPage(musicList);

    // 遍历歌单，把歌单中的歌曲显示到界面上
    for(auto musicId : musicListOfPage)
    {
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end()) {
            continue;
        }
        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMucisName());
        listItemBox->setSinger(it->getSingerName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeIcon(it->getIsLike());

        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
        ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);
    }

    // 更新完成后刷新一下界面
    repaint();
}
